<?php

// +----------------------------------------------------------------------
// |仓库管理控制器
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\BaseController;
use think\facade\View;
use app\common\system\StroagesService;
use app\common\Model\StroageModel;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Stroages extends BaseController
{
    // 仓库列表
    public function index()
    { 
        if(request()->isPost()){
            $param = request()->post();
            $data= StroagesService::list($param);        
            success($data);
        }else{
          return View::fetch();
        }      
    }

    // 仓库添加与修改
    public function create()    {
       
        if(request()->isPost()){
            $param = request()->post();
            $data= StroagesService::create($param);        
            success($data);
        }
    }

    // 仓库删除
    public function delete()
    {
        if(request()->isPost()){
            $param = request()->post(); 
            $data= StroagesService::delete($param);        
            success($data);
        }
    }



     // 库存日志列表
     public function inventorylog()
     {
          //查询仓库        
          $Model=new StroageModel();
          $storagelist=   $Model->getActiveStatus();  //获取仓库     

         
         if(request()->isPost()){
             $param = request()->post();           
             $data= StroagesService::inventorylog($param);        
             success($data);
         }else{
            View::assign('log_type', json_encode($Model->getAllLogTypes()));//库存变化类型
            View::assign('status', json_encode($Model->getAllStatus()));//库存状态类型
            View::assign('storages', json_encode($storagelist));
            return View::fetch();
          }          
     }

     //库存日志导出 
     public function inventorylogExport(){
        
         $param = request()->get();     
         $data= StroagesService::inventorylog_export($param);  
         $title = '库存日志';
        
         $field= [
            'A' => array('name', '仓库名称'),
            'B' => array('nicakname', '操作用户'),
            'C' => array('log_type_name', '库存变化类型'),
            'D' => array('notes', '库存变化说明'),
            'E' => array('localsku', '货号'),
            'F' => array('quantity', '库存变化数量'),
            'G' => array('create_time','库存变化新增时间'),
            'H' => array('operate_time', '库存变化操作时间'),         
        ];
         $width = ['A'=>30,'B'=>25,'C'=>25,'D'=>25,'E'=>40,'F'=>25,'G'=>40,'H'=>40];    
         ExportExcel($data,$field,$title,$width);
       }

    //入库库存表
    public function storageslist(){
         //查询仓库
         $Model=new StroageModel();
         $storagelist=   $Model->getActiveStatus();  //获取仓库     
        
             
        if(request()->isPost()){
            $param = request()->post();           
            $data= StroagesService::storageslist($param);        
            success($data);          
        }else{
            View::assign('storages', json_encode($storagelist));
          return View::fetch();
        }         
    }

    public function storageslistExport(){
        $param = request()->get();     
        $data= StroagesService::storageslist_export($param);  
        $title = '库存清单';      
        $field= [
           'A' => array('name', '仓库名称'),
           'B' => array('localsku', '货号'),
           'C' => array('pwin', '产品条码'),
           'D' => array('category_name', '产品类别'),
           'E' => array('place', '产品货架'),
           'F' => array('actual_price', '产品价格'),
           'G' => array('inventory_quantity','库存数(件)'),
           'H' => array('available_quantity', '可售数(件)'), 
           'L' => array('stock_age', '库龄'),      
           'M' => array('create_time', '入库时间'),   
       ];
        $width = ['A'=>30,'B'=>25,'C'=>20,'D'=>10,'E'=>10,'F'=>10,'G'=>10,'H'=>10,'M'=>40];    
        ExportExcel($data,$field,$title,$width);
    }
      


}
